package com.fom2008;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.fom2008.db.FomTag;

public class FomTagListView extends ListActivity {

	/**
	 * The columns we are interested in from the database
	 */
	private static final String[]	PROJECTION	= new String[] { FomTag.Tags._ID, FomTag.Tags.TAGGED_CONTENT };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// If no data was given in the intent (because we were started
		// as a MAIN activity), then use our default content provider.
		// This is handy for instrumentation.
		Intent intent = getIntent();
		if (intent.getData() == null) {
			intent.setData(Uri.parse("content://com.fom2008.db.fomtags/tags"));
		}

		// Inform the list we provide context menus for items
		getListView().setOnCreateContextMenuListener(this);

		Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null, null);

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_view, cursor, new String[] { "content_uri" },
				new int[] { android.R.id.text1 });
		setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		// We start the view activity against the URI
		startActivity(new Intent(Intent.ACTION_VIEW, Uri.withAppendedPath(getIntent().getData(), "" + position)));
		super.onListItemClick(l, v, position, id);
	}

}